<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="javax.crypto.Cipher" %>
<%@ page import="javax.crypto.spec.SecretKeySpec" %>
<%@ page import="java.math.BigInteger" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<%
    JSONObject jsonData = new JSONObject();
    User user = HrmUserVarify.getUser(request, response);
    String userid = user.getUID() + "";
    String url = "http://hr.wxidg.com:8088/mchr/portal/index";
    String key = "36C7311E32067875"; // 加密密钥
    String ssoKey = "ICf7IVwCdK2MODaC";//授权信息
    try {
        ResourceComInfo resourceComInfo = new ResourceComInfo();
        String workcode = Util.null2String(resourceComInfo.getWorkcode(userid));
        String encryptedHex = encryptEmployeeId(workcode, key);
        jsonData.put("ssotoken", ssoKey);
        jsonData.put("ticket", encryptedHex);
        jsonData.put("url", url);
    } catch (Exception e) {
        e.printStackTrace();
    }
    out.print(jsonData.toJSONString());
%>
<%!

    private String encryptEmployeeId(String employeeId, String key) throws Exception {

        // 创建AES密钥
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");

        // 创建Cipher对象
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

        // 加密数据
        byte[] encryptedBytes = cipher.doFinal(employeeId.getBytes(StandardCharsets.UTF_8));

        // 将加密后的字节数组转换为HEX编码
        String hexEncoded = bytesToHex(encryptedBytes);

        return hexEncoded;
    }

    private String bytesToHex(byte[] bytes) {
        BigInteger bi = new BigInteger(1, bytes);
        String hex = bi.toString(16);
        int paddingLength = (bytes.length * 2) - hex.length();
        if (paddingLength > 0) {
            return String.format("%0" + paddingLength + "d", 0) + hex;
        } else {
            return hex;
        }
    }
%>